package ajaxjs.config;

import java.io.File;
import javax.script.ScriptException;
import ajaxjs.Fso;
import ajaxjs.js.JsEngine;

public class StaticConfig extends JsEngine{
	
	/**
	 * 监视文件是否有变化
	 * @param jsFile_fullPath
	 */
	public void watch(String folder, final String[] jsFile_fullPaths){
		for(String jsFile_fullPath : jsFile_fullPaths){
			load(jsFile_fullPath); // 先都读取一次，最开始的
		}
		Fso.monit(folder, new Fso.FileChangeListener() {  
			int i = 0;
			public void fileChanged(File file) {  
				try { 
					i++;
					String fileName = file.getName();
					for(String jsFile_fullPath : jsFile_fullPaths){
						//System.out.println(jsFile_fullPath + "::::::" + fileName);
						if(jsFile_fullPath.indexOf(fileName) != -1 /* simple match */){
							System.out.println("reload 重新加载" + file.getName());
							load(jsFile_fullPath); // reload 重新加载
							break;
						}
					}
					System.out.println("i:::" + i);
				}catch (Throwable e) {  
					e.printStackTrace();  
				}  
			}  
		});		
	}
	
	public static StaticConfig sc = null;
	
	/**
	 * 读取配置
	 * @param key
	 * @return
	 */
	public String get(String key){
		try {
			return eval(key).toString();
		} catch (ScriptException e) {
			System.out.println("读取配置失败！ key：" + key);
			e.printStackTrace();
			return null;
		}
	}
}
